package com.example.applicationview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 测试 path.setFillType
 */
public class PathFillTypeView  extends View {
    private Paint mPaint;
    //模式
    private Path.FillType[] mModes = {
            Path.FillType.WINDING,
            Path.FillType.INVERSE_WINDING,
            Path.FillType.EVEN_ODD,
            Path.FillType.INVERSE_EVEN_ODD,
    };
    //
    private int mTitleIndex = 0;

    public PathFillTypeView(Context context) {
        this(context,null);
    }

    public PathFillTypeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PathFillTypeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    /** 初始化画笔 */
    private void initPaint(){
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        path.setFillType(mModes[mTitleIndex]);
        path.addCircle(100,100,100, Path.Direction.CCW);
        path.addCircle(150,150,100, Path.Direction.CCW);
        canvas.drawPath(path,mPaint);
    }

    //切换模式 并返回现在的模式名
    public String change() {
        mTitleIndex++;
        if (mTitleIndex >= mModes.length){
            mTitleIndex = 0;
        }
        invalidate();
        return mModes[mTitleIndex].name();
    }
}